var main = angular.module('main');

main.controller('signupCtrl', ['$scope', 'http','$state', function($scope, http,$state) {
    $scope.emailError = false;
    $scope.userNameError = false;
    $scope.passwordError = false;
    $scope.passwordAgainError = false;
    $scope.emai='';
    $scope.userName='';
    $scope.password='';
    $scope.passwordAgain='';
    $scope.checkEmail = function() {
        var email = $scope.email;
        if (!$scope.email.match(/^([a-zA-Z0-9_])+@([a-zA-Z0-9_])+\.([a-zA-Z0-9_])+$/)) {
            $scope.emailError = true;
            $scope.emailErrorMsg = '邮箱格式不正确';
            return false;
        }
        $scope.emailError = false;
        return true;
    };
    $scope.checkUsername = function() {
        var username = $scope.userName;

        if (!$scope.userName.match(/^[a-zA-Z0-9_]+$/)) {
            $scope.userNameError = true;
            $scope.usernameErrorMsg = '用户名格式不正确';
            return false;
        }
        if (!(username.length >= 6 && username.length <= 18)) {
            $scope.userNameError = true;
            $scope.usernameErrorMsg = '用户名长度不正确';
            return false;
        }
        $scope.userNameError = false;
        return true;
    };
    $scope.checkPassword = function() {
        var password = $scope.password;
        if (!(password.length >= 6 && password.length <= 18)) {
            $scope.passwordError = true;
            $scope.passwordErrorMsg = '密码长度不正确';
            return false;
        } 
        if (!$scope.password.match(/^[a-zA-Z0-9_]+$/)) {
            $scope.passwordError = true;
            $scope.passwordErrorMsg = '密码格式不正确';
            return false;
        } else
        $scope.passwordError = false;
        return true;
    };
    $scope.checkPasswordAgain = function() {
        if ($scope.password != $scope.passwordAgain) {
            $scope.passwordAgainError = true;
            $scope.passwordAgainErrorMsg = '两次输入密码不一致';
            return false;
        }
        $scope.passwordAgainError = false;
        return true;
    };

    $scope.signup = function() {
        console.log('in signup');
        if ($scope.checkEmail() && $scope.checkUsername() && $scope.checkPassword() && $scope.checkPasswordAgain()) {
            console.log('in check');
            var result = http.post('/user/signup', { email: $scope.email, username: $scope.userName, password: $scope.password });
            result.then(function (data) {
                console.log(data);
                if(data.code===1){
                    $scope.emailError=true;
                    $scope.emailErrorMsg='对不起，该邮箱已经注册';
                }
                if(data.code===2){
                    $scope.userNameError=true;
                    $scope.usernameErrorMsg='对不起，该用户名已被注册';
                }
                if(data.code===0){
                    $state.go('email_confirm');
                }
            },function (error) {
                console.log(data);
            });
        }
    };
}]);